package com.wxzz.elearing.user.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.*;
import com.wxzz.elearing.user.entity.AdminUser;
import com.wxzz.elearing.user.entity.LoginLog;
import com.wxzz.elearing.user.entity.OperationLog;
import com.wxzz.elearing.user.mapper.OperationLogMapper;
import com.wxzz.elearing.user.service.IAdminUserService;
import com.wxzz.elearing.user.service.IWxUserService;
import com.wxzz.elearing.user.service.impl.AdminUserServiceImpl;
import com.wxzz.elearning.common.bean.BaseEntity;
import com.wxzz.elearning.common.dto.BasePage;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/admin-user")
@Api(tags = "后台用户接口(后台)")
public class AdminUserController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IWxUserService wxUserService;

    @ApiOperation(value = "后台用户登录", notes = "后台用户登录", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "adminUserLogin", method = RequestMethod.POST)
    public ReturnVO<AdminUser> adminUserLogin(@RequestBody AdminLoginDto adminLoginDto) {
        return adminUserService.adminUserLogin(adminLoginDto);
    }

    @ApiOperation(value = "修改微信用户信息", notes = "修改微信用户信息", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "updateWxUser", method = RequestMethod.POST)
    public ReturnVO updateWxUser(@RequestBody UpdateWxUserDto updateWxUserDto) {
        return wxUserService.updateWxUser(updateWxUserDto);
    }

    @ApiOperation(value = "后台账号列表", notes = "后台账号列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listAdminUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<AdminUser>>> listAdminUser(HttpServletResponse response, @RequestHeader String token, @RequestBody AdminUserDto adminUserDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return adminUserService.listAdminUser(response, adminUser.getEnterpriseId(), adminUserDto);
    }

    @ApiOperation(value = "新增或修改后台账号", notes = "新增或修改后台账号", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveAdminUser", method = RequestMethod.POST)
    public ReturnVO<AdminUser> saveAdminUser(@RequestHeader String token, @RequestBody AdminUserInsDto adminUserInsDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return adminUserService.saveAdminUser(adminUser.getEnterpriseId(), adminUserInsDto);
    }

    @ApiOperation(value = "修改当前后台账号", notes = "修改当前后台账号", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveCurAdminUser", method = RequestMethod.POST)
    public ReturnVO<AdminUser> saveCurAdminUser(@RequestHeader String token, @RequestBody AdminUserInsDto adminUserInsDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        adminUserInsDto.setId(adminUser.getId());
        return adminUserService.saveAdminUser(adminUser.getEnterpriseId(), adminUserInsDto);
    }

    @ApiOperation(value = "列出登录日志", notes = "列出登录日志", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listLoginLog", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<LoginLog>> listLoginLog(@RequestHeader String token, @RequestBody LoginLogDto loginLogDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        return adminUserService.listLoginLog(adminUser.getEnterpriseId(), loginLogDto);
    }

    @Autowired
    private OperationLogMapper operationLogMapper;

    @ApiOperation(value = "列出操作日志", notes = "列出操作日志", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listOperationLog", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<OperationLog>> listOperationLog(@RequestHeader String token, @RequestBody OperationLogDto operationLogDto) {
        AdminUser adminUser = JSONObject.parseObject(redisTemplate.opsForValue().get(token), AdminUser.class);
        PageHelper.startPage(operationLogDto.getCurPage(), operationLogDto.getMaxPage());
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("enterprise_id", adminUser.getEnterpriseId());
        if (StringUtils.isNotBlank(operationLogDto.getUsername())) {
            queryWrapper.like("username", operationLogDto.getUsername());
        }
        List<OperationLog> operationLogs = operationLogMapper.selectList(queryWrapper);
        return new ReturnVO<>("列出操作日志成功", new JgPageVo<>(new PageInfo(operationLogs)));
    }


    @ApiOperation(value = "密码找回发送验证码", notes = "密码找回发送验证码", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "sendCode", method = RequestMethod.POST)
    public ReturnVO sendCode(@Valid @RequestBody SendCodeDto sendCodeDto) {
        return adminUserService.sendCode(sendCodeDto);
    }

    @ApiOperation(value = "根据账号修改密码", notes = "根据账号修改密码", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updatePassword", method = RequestMethod.POST)
    public ReturnVO updatePassword(@Valid @RequestBody UpdatePasswordDto updatePasswordDto) {
        return adminUserService.updatePassword(updatePasswordDto);
    }

    @ApiOperation(value = "帐号管理删除", notes = "帐号管理删除", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteByAdminUserId/{adminUserId}", method = RequestMethod.POST)
    public ReturnVO deleteByAdminUserId(@PathVariable Long adminUserId) {
        return adminUserService.deleteByAdminUserId(adminUserId);
    }
}
